<!--
 * @Author       : 邱狮杰
 * @Date         : 2021-08-31 17:18:38
 * @LastEditTime : 2021-09-10 18:13:40
 * @FilePath     : /newhope/src/components/message/index.vue
 * @Description  : 
-->
<script lang="ts" setup>
import { Mask, Share } from '/@/components'
import MessageBox from "/@/assets/images/userMessage.png";
import qrimg from '/@/assets/images/qrimg.png'
import { onUnmounted } from 'vue'
import Title from "/@/assets/images/title/polarMessage.png";
import clickToDraw from "/@/assets/images/clickToDraw.png";
import { useStore } from 'vuex'
// import wx from 'weixin-js-sdk'
import { useBus } from "/@/hooks/bus";
import { longPress } from './utils'
import { useGo } from "/@/hooks/go";
import { useHttp } from '/@/hooks/http'
import { withDefaults, onMounted, ref } from "vue";
const store = useStore()
const bus = useBus();
const [go] = useGo();
const [http] = useHttp()
const shows = ref(false);
const showNextButton = ref(false)
const props = withDefaults(defineProps<{ titleImg: string }>(), {
  titleImg: Title,
});
const showMask = ref(false)
// localStorage.getItem("content") ||
const text = localStorage.getItem("content");
const textareaText = ref(text || "");
const showImage = ref("");
const showShare = ref(false)
const showShareOnece = ref(false)
const isShare = ref(false)
const isLongTouch = ref(false)
const runLoopLongTouch = ref<any>(null)
// const
onMounted(async () => {

  const result = await http({
    method: "GET",
    params: {
      i: 3,
      logout: 1,
      action: "index",
    },
  }) as any
  // title imgUrl
  console.log(result.data.data.desc)
  // @ts-ignore
  wx.ready(function () {
    // @ts-ignore
    wx.onMenuShareAppMessage({
      title: result.data.data.title,
      desc: result.data.data.desc,
      imgUrl: result.data.data.imgUrl,
      link: result.data.data.link,
      success() {
        //@ts-ignore
        bus.emit("showLogo", {});
        bus.emit("showMusic", { show: true });
        go({ name: "bestWishesSuccess" });

      },
      cancel() {
        console.log('errShareMenu')
        showShare.value = true
      }
    })
    // @ts-ignore
    wx.onMenuShareTimeline({
      success() {
        bus.emit("showLogo", {});
        bus.emit("showMusic", { show: true });
        go({ name: "bestWishesSuccess" });
      },
      cancel() {
        console.log('errShare')
        showShare.value = true
      }
    })
  })

  window.scrollTo({
    left: 0,
    top: 0,
    behavior: 'smooth'
  })
  bus.emit("showMessage", { show: false });
  bus.emit("showMusic", { show: false });
  setTimeout(() => {
    bus.emit("print", {});
  }, 0);
  bus.on("showMessage", ({ show }: { show: boolean }) => {
    shows.value = show;
  });
  bus.on("showImage", ({ url }: { url: string }) => {
    showImage.value = url;
  });
  // runLoopLongTouch.value = setInterval(() => {
  //   if (isLongTouch.value) {
  //     if (!showShareOnece.value) {
  //       showMask.value = true
  //       showShare.value = true
  //     }
  //   }
  // }, 5000)
  timer.value = setTimeout(() => {
    if (!showShareOnece.value) {
      showMask.value = true
      showShare.value = true
    }
    // if (!showShareOnece.value) {
    //   showMask.value = true
    //   showShare.value = true
    // }
    // @ts-ignore
  }, 8000)
});
onUnmounted(() => {
  clearInterval(runLoopLongTouch)
  clearTimeout(timer.value)
})
function save() {
  // bus.emit("showMessage", { show: false });
  // setTimeout(() => {
  //   bus.emit("print", {});
  // }, 0);
  // go({ name: "bestWishesSuccess" });
}
function goNext() {
  bus.emit("showLogo", {});
  bus.emit("showMusic", { show: true });
  go({ name: "bestWishesSuccess" });
}
const timer = ref<any>()
function touchStart() {
  timer.value = setTimeout(() => {
    isLongTouch.value = true
    // if (!showShareOnece.value) {
    //   showMask.value = true
    //   showShare.value = true
    // }
    // @ts-ignore
  }, 8000)
}
function touchend() {
  clearTimeout(timer.value)

}
function maskClick() {
  showMask.value = false
  showNextButton.value = true
  showShare.value = false
  showShareOnece.value = true
}
</script>

<template>
  <Share :style="'z-index:119'" v-if="showShare" />
  <Mask :style="'z-index:109'" v-if="showMask" @click="maskClick" />
  <div v-if="!showImage" class="message">
    <img class="title" alt="message" :src="props.titleImg" />
    <div class="messageBox">
      <div class="userMessagebox">
        <div class="relativeBox">
          <img :src="MessageBox" />
          <div>{{ textareaText }}</div>
        </div>
      </div>
      <div class="longPressToSave">
        <img class="qrcode" @click="save" :src="qrimg" />
        <span class="msg scan" style=" color: #9b0b11; letter-spacing: 0px">扫码领福利</span>
        <!-- <div v-if="!shows" class="msg">
          <span>长按保存</span>
          <span style="font-size: 11px; letter-spacing: 0px">分享你的佳节祝福语</span>
        </div>-->
      </div>
    </div>
  </div>
  <img
    v-if="showImage"
    class="showImage"
    id="showImg"
    :src="showImage"
    @touchstart="touchStart"
    @touchend="touchend"
  />
  <img
    v-if="showNextButton"
    :src="clickToDraw"
    :style="'z-index:109'"
    class="clickToDraww"
    @click="goNext"
  />
  <div v-if="showImage" class="longPressToSave left">
    <!-- <img class="qrcode" @click="save" :src="store.state.user.qrimg" /> -->
    <!-- <div class="msg">
      <span>扫码领福利</span>
    </div>-->
    <div class="msg">
      <span>长按保存</span>
      <span style="font-size: 11px; letter-spacing: 0px">分享你的佳节祝福语</span>
    </div>
  </div>
  <!-- <div class="longPressToSave" v-if="showImage"> -->
  <!-- <div class="qrcode" @click="goNext"></div> -->
  <!-- <img v-if="showImage" :src="clickToDraw" class="clickToDraww" /> -->
  <!-- <div class="msg"> -->
  <!-- <span>长按保存</span> -->
  <!-- <span style="font-size: 11px; letter-spacing: 0px">分享你的佳节祝福语</span> -->
  <!-- </div> -->
  <!-- </div> -->
  <!-- <img v-if="showImage" :src="clickToDraw" class="clickToDraww" /> -->
</template>

<style lang="scss" scoped>
.scan {
  margin-left: 8px;
  font-weight: bold;
  font-size: 14px;
}
.left {
  bottom: 50px !important;
  left: 129px;
}
.longPressToSave {
  user-select: none;
  position: absolute;
  bottom: 20px;
  display: flex;
  // justify-content: center;
  align-items: center;
  .qrcode {
    user-select: none;
    width: 78px;
    height: 76px;
    // background-color: #ffdb91;
    // box-shadow: 0px 1px 2px 0px #8a050d;
    border: solid 1px #be1728;
    margin: {
      left: 32px;
      right: 13px;
    }
  }
  .msg {
    user-select: none;
    display: flex;
    flex-direction: column;
    align-self: flex-end;
    span {
      // width: 78px;
      font-size: 18px;
      font-weight: normal;
      font-stretch: normal;
      letter-spacing: 4px;
      font-weight: bold;
      color: #9b0b11;
      text-shadow: 0px 1px 2px 0px #ffd06f;
    }
  }
}
.showImage {
  user-select: none;
  position: absolute;
  height: 100%;
  width: 100%;
}
.clickToDraww {
  user-select: none;
  position: absolute;
  bottom: 160px;
  display: flex;
  right: 50px;
  width: 134px;
  height: 32px;
  // justify-content: center;
  align-items: center;
}
.message {
  user-select: none;
  position: absolute;
  height: 100%;
  width: 100%;
  .title {
    user-select: none;
    position: absolute;
    top: 86px;
    left: 50%;
    transform: translateX(-50%);
    width: 336px;
  }
  .messageBox {
    user-select: none;
    position: relative;
    height: 100%;
    width: 100%;

    .userMessagebox {
      user-select: none;
      position: absolute;
      top: 212px;
      left: 50%;
      transform: translateX(-50%);
      width: 317px;
      height: 176px;
      .relativeBox {
        user-select: none;
        position: relative;
        img {
          height: 100%;
          width: 100%;
          object-fit: cover;
        }
        div {
          position: absolute;
          top: 42px;
          left: 50%;
          transform: translateX(-50%);
          height: 126px;
          width: 90%;
          outline: none;
          border: none;
          background: transparent;
          font-size: 16px;
          font-weight: bold;
          font-stretch: normal;
          // line-height: 34px;
          letter-spacing: 2px;
          color: #bd010c;
          //   text-align: center;
        }
      }
    }
  }
}
</style>
